package com.lsx143.wordcount.day3.fight;

import com.lsx143.wordcount.bean.UserBehavior;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

public class Test_PV {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env
                .readTextFile("input/UserBehavior.csv")
                .map(new MapFunction<String, UserBehavior>() {
                    @Override
                    public UserBehavior map(String line) throws Exception {
                        String[] datas = line.split(",");
                        return new UserBehavior(
                                Long.parseLong(datas[0]),
                                Long.parseLong(datas[1]),
                                Integer.parseInt(datas[2]),
                                datas[3],
                                Long.parseLong(datas[4])
                        );
                    }
                })
                .filter(new FilterFunction<UserBehavior>() {
                    @Override
                    public boolean filter(UserBehavior userBehavior) throws Exception {
                        return "pv".equals(userBehavior.getBehavior());
                    }
                })
                .process(new ProcessFunction<UserBehavior, Integer>() {
                    int sum = 0;

                    @Override
                    public void processElement(UserBehavior userBehavior, Context ctx, Collector<Integer> out) throws Exception {
                        out.collect(++sum);
                    }
                })
                .print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
